<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
* @author Radu Buzea
* @copyright Copyright (c) 2009-2010, Radu Buzea (www.hypnosis.ro)
* @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
* @package HypnoPHP
* @category Core
* @version 1.0.3
*/

// ============================================================================
// Class: HBaseDbField
// ============================================================================

abstract class HBaseDbField
{

	protected $__name = '';
	protected $__table_name = '';
	protected $__schema = '';
	protected $__type = '';
	protected $__length = 0;
	protected $__properties = null;

	# ###########################################################################

	/**
	* Abstract method
	* @return mixed - validated data
	*/
	abstract public function validateData($data = null, &$error_text = null);

	/**
	* Abstract method
	* @return string - code used in HBaseDbTable::write_cache_structure
	*/
	abstract public function generateCahceCode();

	# ###########################################################################

	/**
	* Class constructor
	*
	* @param mixed $options
	* @return HBaseDbField
	*/
	public function __construct($options = null)
	{
		if (is_array($options)) {
			foreach ($options as $key => $value) {
				$key = strtolower($key);
				switch ($key) {
					case 'field':
					case 'field_name':
					case 'name':
						$this->__name = trim($value);
						break;
					case 'table':
					case 'table_name':
						$this->__table_name = trim($value);
						break;
					case 'schema':
					case 'schema_name':
						$this->__schema = trim($value);
						break;
					case 'type':
					case 'field_type':
						$this->__type = strtoupper(trim($value));
						break;
					case 'field_len':
					case 'field_length':
					case 'field_size':
					case 'len':
					case 'length':
					case 'size':
						$this->__length = intval($value);
						break;
					default:
						if (!is_object($value) && !is_array($value)) {
							if (is_null($this->__properties)) {
								$this->__properties = array();
							}
							$this->__properties[$key] = $value;
						}
				}
			}
		}
		if (method_exists($this, '__initialize')) { $this->__initialize($options); }
	}

	# ###########################################################################

	/**
	* Class destructor
	*
	* @return void
	*/
	public function __destruct()
	{
		unset($this);
	}

	# ###########################################################################

	/**
	* Global members overloading - Getter method
	*
	* @param string $param The object property name
	*/
	public function __get($param = '')
	{
		$param = strtolower($param);
		switch ($param) {
			case 'name':
			case 'field_name':
				return $this->__name;
			case 'table':
			case 'table_name':
				return $this->__table_name;
			case 'schema':
			case 'schema_name':
				return $this->__schema;
			case 'type':
			case 'field_type':
				return $this->__type;
			case 'length':
			case 'field_length':
				return $this->__length;
			case 'properties':
			case 'field_properties':
			case 'field_props':
				return $this->__properties;
			default:
				if (is_null($this->__properties)) { return null; }
				return isset($this->__properties[$param]) ? $this->__properties[$param] : null;
		}
	}

	# ###########################################################################

	/**
	* Global members overloading - Setter method
	*
	* @param string $param The object member name
	* @param mixed $value The object member value
	*/
	public function __set($param = '', $value = null)
	{
		$param = strtolower($param);
		switch ($param) {
			case 'name':
			case 'field_name':
				$this->__name = is_string($value) ? trim($value) : '';
				break;
			case 'table':
			case 'table_name':
				$this->__table_name = is_string($value) ? strtolower(trim($value)) : '';
				break;
			case 'schema':
			case 'schema_name':
				$this->__schema = is_string($value) ? strtolower(trim($value)) : '';
				break;
			case 'type':
			case 'field_type':
				$this->__type = is_string($value) ? strtolower(trim($value)) : '';
				break;
			case 'length':
			case 'field_length':
				$this->__length = is_null($value) ? 0 : intval($value);
				break;
			case 'properties':
			case 'field_properties':
			case 'field_props':
				if (is_null($this->__properties)) {
					$this->__properties = array();
				}
				$this->__properties = is_array($value) ? $value : array();
				break;
			default:
				if (is_null($this->__properties)) {
					$this->__properties = array();
				}
				$this->__properties[$param] = $value;
		}
	}

	# ###########################################################################

}
